package com.jolink.omiis.wechat.api.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jolink.omiis.wechat.api.IUserOperationApi;
import com.jolink.omiis.wechat.keys.IPreferences;

public class UserOperationApi<T> extends WeChatApiBaseService implements IUserOperationApi {
	private static Logger logger = LoggerFactory.getLogger(UserOperationApi.class);

	@SuppressWarnings("unchecked")
	@Override
	public T queryUserInfo(String openId, String... values) {
		long t1 = System.currentTimeMillis();
		logger.info("调用腾讯获取客户基本信息接口......");
		String app_id = getContext().getAppIdByUserName(values[0]);
		Map<String, String> companyInfo = getContext().getCompanyInfo(IPreferences.COMPANY_REDIS_PREFIX + "_" + app_id);
		logger.info("微信公众号：{}，基本信息：{}。", app_id, companyInfo);
		String access_token = companyInfo.get("authorizer_access_token");
		String uri = USER_INFO;
//		String uri = "https://api.weixin.qq.com/cgi-bin/user/info";
		Map<String, String> reqParams = new HashMap<>();
		reqParams.put("access_token", access_token);
		reqParams.put("openid", openId);
		reqParams.put("lang", "zh_CN");
		String result = null;
		try {
			result = (String) executeGet(uri, reqParams);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		long t2 = System.currentTimeMillis();
		logger.info("调用腾讯获取客户基本信息接口完成，结果：{}，耗时：{}毫秒。", result, (t2 - t1));
		return (T) result;
	}

}
